<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="24">

          <a-col :md="6" :sm="10">
            <a-form-item label="操作人">
              <a-input placeholder="请输入操作人" v-model="queryParam.userName"></a-input>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="10">
            <a-form-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-range-picker
                style="width: 210px"
                v-model="queryParam.createTimeRange"
                format="YYYY-MM-DD"
                :placeholder="['开始时间', '结束时间']"
                @change="onDateChange"
                @ok="onDateOk"
              />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="10" style="left: 40px">
            <a-form-item label="操作类型">
              <a-select v-model="queryParam.operateType" style="width: 120px" placeholder="请选择操作类型" @change="handleChange">
                <a-select-option value="1">查询</a-select-option>
                <a-select-option value="2">添加</a-select-option>
                <a-select-option value="3">更新</a-select-option>
                <a-select-option value="4">删除</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :md="6" :sm="24">
                <a-button type="primary" style="left: 10px" @click="searchQuery" icon="search">查询</a-button>
                <a-button type="primary" @click="searchReset" icon="reload"
                          style="margin-left: 8px;left: 10px">重置</a-button>
            </a-col>
          </span>

        </a-row>
      </a-form>
    </div>

    <!-- table区域-begin -->
    <a-table
      ref="table"
      size="middle"
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange">

      <div slot="expandedRowRender" slot-scope="record" style="margin: 0">
        <div style="margin-bottom: 5px">
          <a-badge status="success" style="vertical-align: middle;"/>
          <span style="vertical-align: middle;">请求方法:{{ record.requestMethod }}</span>
        </div>
        <div>
          <a-badge status="default" style="vertical-align: middle;"/>
          <span style="vertical-align: middle;">请求URL:{{ record.actionUrl }}</span>
        </div>
        <div>
          <a-badge status="processing" style="vertical-align: middle;"/>
          <span style="vertical-align: middle;">请求参数:{{ record.params }}</span>
        </div>
      </div>
      <!-- 字符串超长截取省略号显示-->
      <span slot="logContent" slot-scope="text, record">
          <!--<j-ellipsis :value="text" :length="40"/>-->
        </span>
    </a-table>
    <!-- table区域-end -->
  </a-card>
</template>

<script>
  import { filterObj } from '@/utils/util'
  import { queryLogList } from '@/api/log'

  export default {
    name: 'Log',
    components: {
    },
    data () {
      return {
        // 查询条件
        queryParam: {
        },
        dataSource: [],
        loading: false,
        /* 排序参数 */
        sorter: {
          column: 'createTime',
          order: 'desc'
        },
        pagination: {
          current: 1,
          pageSize: 10,
          pageSizeOptions: ['10', '20', '30'],
          showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
          },
          showQuickJumper: true,
          showSizeChanger: true,
          total: 0
        },
        tabKey: '1',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            align: 'center',
            customRender: function (t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '操作人名称',
            dataIndex: 'userName',
            align: 'center',
            sorter: true
          },
          {
            title: '描述',
            dataIndex: 'description',
            align: 'center',
            sorter: true
          },
          {
            title: 'IP',
            dataIndex: 'requestIp',
            align: 'center',
            sorter: true
          },
          {
            title: '耗时(毫秒)',
            dataIndex: 'consumingTime',
            align: 'center',
            sorter: true
          },
          {
            title: '日志类型',
            dataIndex: 'type',
            customRender: function (text) {
              if (text == 1) {
                return '正常'
              } else if (text == 2) {
                return '异常'
              } else {
                return text
              }
            },
            align: 'center'
          },
          {
            title: '创建时间',
            dataIndex: 'startTime',
            align: 'center',
            sorter: true
          }
        ],
        operateColumn:
          {
            title: '操作类型',
            dataIndex: 'operateType_dictText',
            align: 'center'
          },
        labelCol: {
          xs: { span: 1 },
          sm: { span: 2 }
        },
        wrapperCol: {
          xs: { span: 10 },
          sm: { span: 16 }
        }
      }
    },
    created () {
      this.loadData()
    },
    methods: {
      loadData () {
        //加载数据
        let params = this.getQueryParams()//查询条件
        this.loading = true
        queryLogList(params).then((res) => {
          console.log(res)
          if (res.code == 200) {
            this.dataSource = res.data.records
            this.pagination.total = res.data.total
          } else {
            this.$message.warning(res.message)
          }
          this.loading = false
        })
      },
      getQueryParams () {
        console.log(this.queryParam.createTimeRange)
        let param = Object.assign({}, this.queryParam)
        param.current = this.pagination.current
        param.size = this.pagination.pageSize
        delete param.createTimeRange // 时间参数不传递后台
        return filterObj(param)
      },

      handleTableChange (pagination, filters, sorter) {
        if (Object.keys(sorter).length > 0) {
          this.sorter.column = sorter.field
          this.sorter.order = 'ascend' == sorter.order ? 'asc' : 'desc'
        }
        console.log(this.sorter)
        this.pagination = pagination
        this.loadData()
      },
      // 重置
      searchReset () {
        this.queryParam = {} //清空查询区域参数
        this.loadData()
      },
      // 日志类型
      callback (key) {

        // 动态添加操作类型列
        if (key == 2) {
          this.tabKey = '2'
          this.columns.splice(7, 0, this.operateColumn)
        } else if (this.columns.length == 9) {
          this.tabKey = '1'
          this.columns.splice(7, 1)
        }

        let that = this
        that.queryParam.type = key
        that.loadData()
      },
      onDateChange: function (value, dateString) {
        console.log(dateString[0], dateString[1])
        this.queryParam.createTime_begin = dateString[0]
        this.queryParam.createTime_end = dateString[1]
      },
      searchQuery () {
        this.loadData()
      },
      onDateOk (value) {
        console.log(value)
      },
      handleChange(value) {
        this.queryParam.operateType = value;
      }
    }
  }
</script>
<style scoped>
</style>